package com.lining.orangeservice.entity.abstractentity.impl;

import com.lining.orangeservice.entity.abstractentity.Order;
import com.lining.orangeservice.entity.po.OrderItem;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 秒杀订单
 */
public class SeckillOrder extends Order {
    private Long seckillActivityId;
    private BigDecimal seckillPrice;
    private LocalDateTime seckillStartTime;
    private LocalDateTime seckillEndTime;
    
    @Override
    public void calculateAmount() {
        if (items == null || items.isEmpty()) {
            totalAmount = BigDecimal.ZERO;
            discountAmount = BigDecimal.ZERO;
            actualAmount = BigDecimal.ZERO;
            return;
        }
        
        // 秒杀订单使用秒杀价
        totalAmount = items.stream()
            .map(item -> seckillPrice.multiply(new BigDecimal(item.getQuantity())))
            .reduce(BigDecimal.ZERO, BigDecimal::add);
        
        // 计算节省金额（原价-秒杀价）
        BigDecimal originalTotal = items.stream()
            .map(OrderItem::getSubTotal)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
        discountAmount = originalTotal.subtract(totalAmount);
        
        actualAmount = totalAmount;
    }
    
    @Override
    public boolean canCancel() {
        // 秒杀订单不能取消
        return false;
    }
    
    @Override
    public boolean canRefund() {
        // 秒杀订单不能退款
        return false;
    }
    
    public boolean isValidSeckillTime() {
        LocalDateTime now = LocalDateTime.now();
        return now.isAfter(seckillStartTime) && now.isBefore(seckillEndTime);
    }

    // getters and setters
    public Long getSeckillActivityId() {
        return seckillActivityId;
    }

    public void setSeckillActivityId(Long seckillActivityId) {
        this.seckillActivityId = seckillActivityId;
    }

    public BigDecimal getSeckillPrice() {
        return seckillPrice;
    }

    public void setSeckillPrice(BigDecimal seckillPrice) {
        this.seckillPrice = seckillPrice;
    }

    public LocalDateTime getSeckillStartTime() {
        return seckillStartTime;
    }

    public void setSeckillStartTime(LocalDateTime seckillStartTime) {
        this.seckillStartTime = seckillStartTime;
    }

    public LocalDateTime getSeckillEndTime() {
        return seckillEndTime;
    }

    public void setSeckillEndTime(LocalDateTime seckillEndTime) {
        this.seckillEndTime = seckillEndTime;
    }
}